package com.yanqu.road.dao.impl.union.cross.huaben;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.union.huaben.UnionHuaBenPass;
import com.yanqu.road.entity.union.huaben.UnionHuaBenPassWeekData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionHuaBenPassWeekDataDaoImpl extends TempDataObjectDao<UnionHuaBenPassWeekData> {

    public static String getUnionHuaBenPassWeekDataSql = "select * from `t_c_hua_ben_pass_week` where `weekId` = ? or `weekId` = ? ";
    public Map<Integer, Map<Long, Map<String, UnionHuaBenPassWeekData>>> getUnionHuaBenPassWeekData(int thisWeedId, int preWeedId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Long, Map<String, UnionHuaBenPassWeekData>>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getUnionHuaBenPassWeekDataSql);
            int i = 1;
            call.setInt(i++, thisWeedId);
            call.setInt(i++, preWeedId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionHuaBenPassWeekData data = parse(rs);
                if(result.get(data.getWeekId()) == null){
                    result.put(data.getWeekId(), new ConcurrentHashMap<>());
                }
                Map<Long, Map<String, UnionHuaBenPassWeekData>> weekMap = result.get(data.getWeekId());

                if(weekMap.get(data.getServerId()) == null){
                    weekMap.put(data.getServerId(), new ConcurrentHashMap<>());
                }

                Map<String, UnionHuaBenPassWeekData> serverMap = weekMap.get(data.getServerId());

                serverMap.put(data.getUnionUid(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertUnionHuaBenPassWeekDataSql = "insert into `t_c_hua_ben_pass_week`(unionUid,weekId,serverId,degree,openTime,closeTime,lastPassTime,duration,pingJia,passList) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UnionHuaBenPassWeekData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDegree()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOpenTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCloseTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLastPassTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDuration()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPingJia()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPassList())));
        if (getDb().executeNoneQuery(insertUnionHuaBenPassWeekDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUnionHuaBenPassWeekDataSql = "update `t_c_hua_ben_pass_week` set serverId=?,degree=?,openTime=?,closeTime=?,lastPassTime=?,duration=?,pingJia=?,passList=? where unionUid=? and weekId=?";

    @Override
    protected boolean updateData(UnionHuaBenPassWeekData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDegree()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getOpenTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCloseTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLastPassTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDuration()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPingJia()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPassList())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        if (getDb().executeNoneQuery(updateUnionHuaBenPassWeekDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UnionHuaBenPassWeekData parseData(ResultSet rs) throws SQLException {
        UnionHuaBenPassWeekData data = new UnionHuaBenPassWeekData();
        data.setUnionUid(rs.getString("unionUid"));
        data.setWeekId(rs.getInt("weekId"));
        data.setServerId(rs.getLong("serverId"));
        data.setDegree(rs.getInt("degree"));
        data.setOpenTime(rs.getInt("openTime"));
        data.setCloseTime(rs.getInt("closeTime"));
        data.setLastPassTime(rs.getInt("lastPassTime"));
        data.setDuration(rs.getInt("duration"));
        data.setPingJia(rs.getInt("pingJia"));
        data.setPassList(JSON.parseObject(rs.getString("passList"), new TypeReference<List<UnionHuaBenPass>>() {}));

        return data;
    }
}
